/*
	Title: 02_Table4.do
	Paper: Brauning, Ivashina, Ozdagli (2025)
	Description: Table 4 - Incidence of incurrence covenants in leveraged loans
*/

/*------------------------------------------------------------------------------
								  Table 4
------------------------------------------------------------------------------*/
// Import Table 4 data
use "$data_path/Table4.dta", clear 


// Table Setup -----------------------------------------------------------------
putdocx clear 
putdocx begin
putdocx paragraph
putdocx table table4 = (18, 7), border(insideV, nil) border(insideH, nil) border(left, nil) border(right, nil)

* Left margin
putdocx table table4(4,1) = ("High-growth industry")
putdocx table table4(7,1) = ("High corporate govern. index")
putdocx table table4(10,1) = ("High correl. govern. index")
putdocx table table4(13,1) = ("Constant")
putdocx table table4(14,.), border(bottom)
putdocx table table4(15,1) = ("Obs.")
putdocx table table4(16,1) = ("R-squared")
putdocx table table4(17,1) = ("Mean incur.")
putdocx table table4(18,1) = ("Time FE")

* Top margin
putdocx table table4(1,2), colspan(8)
putdocx table table4(1,2) = ("Dependent variable is 1 if loan has incurrence covenant")
putdocx table table4(1,2), border(bottom)

putdocx table table4(2,2) = ("Any incurrence")
putdocx table table4(2,3) = ("Incurrence w/ invest/acq. restriction")
putdocx table table4(2,4) = ("Any incurrence")
putdocx table table4(2,5) = ("Incurrence w/ invest/acq. restriction")
putdocx table table4(2,6) = ("Any incurrence")
putdocx table table4(2,7) = ("Incurrence w/ invest/acq. restriction")

putdocx table table4(3,2) = ("(1)")
putdocx table table4(3,3) = ("(2)")
putdocx table table4(3,4) = ("(3)")
putdocx table table4(3,5) = ("(4)")
putdocx table table4(3,6) = ("(5)")
putdocx table table4(3,7) = ("(6)")
putdocx table table4(3,.), border(bottom)


// Column (1) ------------------------------------------------------------------
// Regression: 
reghdfe incurrence high_sales_growth_naics_rw_p90, ///
				   absorb(qdate) vce(robust)
							  
// Store results 
matrix reg_results = r(table)
local b : display %04.3f reg_results["b", "high_sales_growth_naics_rw_p90"]  
local se : display %04.2f reg_results["se", "high_sales_growth_naics_rw_p90"]
local constant : display %04.3f reg_results["b", "_cons"]
local constant_se : display %04.2f reg_results["se", "_cons"]

local p = reg_results["pvalue", "high_sales_growth_naics_rw_p90"]
local p_cons = reg_results["pvalue", "_cons"]

if (`p' > 0.1) {
	local p_sigstars = ""
} 
if (`p' < 0.1) {
	local p_sigstars = "*"
}
if (`p' < 0.05) {
	local p_sigstars = "**"
}
if (`p' < 0.01) {
	local p_sigstars = "***"
}

if (`p_cons' > 0.1) {
	local p_cons_sigstars = ""
} 
if (`p_cons' < 0.1) {
	local p_cons_sigstars = "*"
}
if (`p_cons' < 0.05) {
	local p_cons_sigstars = "**"
}
if (`p_cons' < 0.01) {
	local p_cons_sigstars = "***"
}
local N = e(N)
local r2 : display %9.2f  e(r2)

qui sum incurrence if e(sample)
local mean_incur : display %04.3f r(mean)

// Put results into table in Word Document 
putdocx table table4(4,2) = ("`b'`p_sigstars'")
putdocx table table4(5,2) = ("(`se')")
putdocx table table4(13,2) = ("`constant'`p_sigstars'")
putdocx table table4(14,2) = ("(`constant_se')")
putdocx table table4(15,2) = (`N')
putdocx table table4(16,2) = (`r2')
putdocx table table4(17,2) = (`mean_incur')
putdocx table table4(18,2) = ("Yes")


// Column (2) ------------------------------------------------------------------
// Regression: 
reghdfe incurrence_ma_in high_sales_growth_naics_rw_p90, ///
					     absorb(qdate) vce(robust)
							  
// Store results 
matrix reg_results = r(table)
local b : display %04.3f reg_results["b", "high_sales_growth_naics_rw_p90"]  
local se : display %04.2f reg_results["se", "high_sales_growth_naics_rw_p90"]
local constant : display %04.3f reg_results["b", "_cons"]
local constant_se : display %04.2f reg_results["se", "_cons"]

local p = reg_results["pvalue", "high_sales_growth_naics_rw_p90"]
local p_cons = reg_results["pvalue", "_cons"]

if (`p' > 0.1) {
	local p_sigstars = ""
} 
if (`p' < 0.1) {
	local p_sigstars = "*"
}
if (`p' < 0.05) {
	local p_sigstars = "**"
}
if (`p' < 0.01) {
	local p_sigstars = "***"
}

if (`p_cons' > 0.1) {
	local p_cons_sigstars = ""
} 
if (`p_cons' < 0.1) {
	local p_cons_sigstars = "*"
}
if (`p_cons' < 0.05) {
	local p_cons_sigstars = "**"
}
if (`p_cons' < 0.01) {
	local p_cons_sigstars = "***"
}
local N = e(N)
local r2 : display %9.2f  e(r2)

qui sum incurrence_ma_in if e(sample)
local mean_incur : display %04.3f r(mean)

// Put results into table in Word Document 
putdocx table table4(4,3) = ("`b'`p_sigstars'")
putdocx table table4(5,3) = ("(`se')")
putdocx table table4(13,3) = ("`constant'`p_cons_sigstars'")
putdocx table table4(14,3) = ("(`constant_se')")
putdocx table table4(15,3) = (`N')
putdocx table table4(16,3) = (`r2')
putdocx table table4(17,3) = (`mean_incur')
putdocx table table4(18,3) = ("Yes")


// Column (3) ------------------------------------------------------------------
// Regression: 
reghdfe incurrence high_E_index_p90, ///
				   absorb(qdate) vce(robust)
				   
// Store results 
matrix reg_results = r(table)
local b : display %04.3f reg_results["b", "high_E_index_p90"]  
local se : display %04.2f reg_results["se", "high_E_index_p90"]
local constant : display %04.3f reg_results["b", "_cons"]
local constant_se : display %04.2f reg_results["se", "_cons"]

local p = reg_results["pvalue", "high_E_index_p90"]
local p_cons = reg_results["pvalue", "_cons"]

if (`p' > 0.1) {
	local p_sigstars = ""
} 
if (`p' < 0.1) {
	local p_sigstars = "*"
}
if (`p' < 0.05) {
	local p_sigstars = "**"
}
if (`p' < 0.01) {
	local p_sigstars = "***"
}

if (`p_cons' > 0.1) {
	local p_cons_sigstars = ""
} 
if (`p_cons' < 0.1) {
	local p_cons_sigstars = "*"
}
if (`p_cons' < 0.05) {
	local p_cons_sigstars = "**"
}
if (`p_cons' < 0.01) {
	local p_cons_sigstars = "***"
}
local N = e(N)
local r2 : display %9.2f  e(r2)

qui sum incurrence if e(sample)
local mean_incur : display %04.3f r(mean)

// Put results into table in Word Document 
putdocx table table4(7,4) = ("`b'`p_sigstars'")
putdocx table table4(8,4) = ("(`se')")
putdocx table table4(13,4) = ("`constant'`p_cons_sigstars'")
putdocx table table4(14,4) = ("(`constant_se')")
putdocx table table4(15,4) = (`N')
putdocx table table4(16,4) = (`r2')
putdocx table table4(17,4) = (`mean_incur')
putdocx table table4(18,4) = ("Yes")


// Column (4) ------------------------------------------------------------------
// Regression: 
reghdfe incurrence_ma_in high_E_index_p90, ///
				         absorb(qdate) vce(robust)
				   
// Store results 
matrix reg_results = r(table)
local b : display %04.3f reg_results["b", "high_E_index_p90"]  
local se : display %04.2f reg_results["se", "high_E_index_p90"]
local constant : display %04.3f reg_results["b", "_cons"]
local constant_se : display %04.2f reg_results["se", "_cons"]

local p = reg_results["pvalue", "high_E_index_p90"]
local p_cons = reg_results["pvalue", "_cons"]

if (`p' > 0.1) {
	local p_sigstars = ""
} 
if (`p' < 0.1) {
	local p_sigstars = "*"
}
if (`p' < 0.05) {
	local p_sigstars = "**"
}
if (`p' < 0.01) {
	local p_sigstars = "***"
}

if (`p' > 0.1) {
	local p_cons_sigstars = ""
} 
if (`p' < 0.1) {
	local p_cons_sigstars = "*"
}
if (`p' < 0.05) {
	local p_cons_sigstars = "**"
}
if (`p' < 0.01) {
	local p_cons_sigstars = "***"
}
local N = e(N)
local r2 : display %9.2f  e(r2)

qui sum incurrence_ma_in if e(sample)
local mean_incur : display %04.3f r(mean)

// Put results into table in Word Document 
putdocx table table4(7,5) = ("`b'`p_sigstars'")
putdocx table table4(8,5) = ("(`se')")
putdocx table table4(13,5) = ("`constant'`p_cons_sigstars'")
putdocx table table4(14,5) = ("(`constant_se')")
putdocx table table4(15,5) = (`N')
putdocx table table4(16,5) = (`r2')
putdocx table table4(17,5) = (`mean_incur')
putdocx table table4(18,5) = ("Yes")


// Column (5) ------------------------------------------------------------------
// Regression: 
reghdfe incurrence high_corr_sgrwth_rw_l8_p90, ///
				   absorb(qdate) vce(robust)
				   
// Store results 
matrix reg_results = r(table)
local b : display %04.3f reg_results["b", "high_corr_sgrwth_rw_l8_p90"]  
local se : display %04.2f reg_results["se", "high_corr_sgrwth_rw_l8_p90"]
local constant : display %04.3f reg_results["b", "_cons"]
local constant_se : display %04.2f reg_results["se", "_cons"]

local p = reg_results["pvalue", "high_corr_sgrwth_rw_l8_p90"]
local p_cons = reg_results["pvalue", "_cons"]

if (`p' > 0.1) {
	local p_sigstars = ""
} 
if (`p' < 0.1) {
	local p_sigstars = "*"
}
if (`p' < 0.05) {
	local p_sigstars = "**"
}
if (`p' < 0.01) {
	local p_sigstars = "***"
}

if (`p_cons' > 0.1) {
	local p_cons_sigstars = ""
} 
if (`p_cons' < 0.1) {
	local p_cons_sigstars = "*"
}
if (`p_cons' < 0.05) {
	local p_cons_sigstars = "**"
}
if (`p_cons' < 0.01) {
	local p_cons_sigstars = "***"
}
local N = e(N)
local r2 : display %9.2f  e(r2)

qui sum incurrence if e(sample)
local mean_incur : display %04.3f r(mean)

// Put results into table in Word Document 
putdocx table table4(10,6) = ("`b'`p_sigstars'")
putdocx table table4(11,6) = ("(`se')")
putdocx table table4(13,6) = ("`constant'`p_cons_sigstars'")
putdocx table table4(14,6) = ("(`constant_se')")
putdocx table table4(15,6) = (`N')
putdocx table table4(16,6) = (`r2')
putdocx table table4(17,6) = (`mean_incur')
putdocx table table4(18,6) = ("Yes")


// Column (6) ------------------------------------------------------------------
// Regression: 
reghdfe incurrence_ma_in high_corr_sgrwth_rw_l8_p90, ///
				         absorb(qdate) vce(robust)
				   
// Store results 
matrix reg_results = r(table)
local b : display %04.3f reg_results["b", "high_corr_sgrwth_rw_l8_p90"]  
local se : display %04.2f reg_results["se", "high_corr_sgrwth_rw_l8_p90"]
local constant : display %04.3f reg_results["b", "_cons"]
local constant_se : display %04.2f reg_results["se", "_cons"]

local p = reg_results["pvalue", "high_corr_sgrwth_rw_l8_p90"]
local p_cons = reg_results["pvalue", "_cons"]

if (`p' > 0.1) {
	local p_sigstars = ""
} 
if (`p' < 0.1) {
	local p_sigstars = "*"
}
if (`p' < 0.05) {
	local p_sigstars = "**"
}
if (`p' < 0.01) {
	local p_sigstars = "***"
}

if (`p_cons' > 0.1) {
	local p_cons_sigstars = ""
} 
if (`p_cons' < 0.1) {
	local p_cons_sigstars = "*"
}
if (`p_cons' < 0.05) {
	local p_cons_sigstars = "**"
}
if (`p_cons' < 0.01) {
	local p_cons_sigstars = "***"
}
local N = e(N)
local r2 : display %9.2f  e(r2)

qui sum incurrence_ma_in if e(sample)
local mean_incur : display %04.3f r(mean)

// Put results into table in Word Document 
putdocx table table4(10,7) = ("`b'`p_sigstars'")
putdocx table table4(11,7) = ("(`se')")
putdocx table table4(13,7) = ("`constant'`p_cons_sigstars'")
putdocx table table4(14,7) = ("(`constant_se')")
putdocx table table4(15,7) = (`N')
putdocx table table4(16,7) = (`r2')
putdocx table table4(17,7) = (`mean_incur')
putdocx table table4(18,7) = ("Yes")


// Table Setup (end) -----------------------------------------------------------
putdocx table table4(1 2/18.,2/7), halign(center)
putdocx table table4(.,1), width(3 in)

* Number formatting
putdocx table table4(15,2/7), nformat(%9.0fc)
putdocx table table4(16,2/7), nformat(%9.2g)
putdocx table table4(17,2/7), nformat(%04.3f)

* Table formatting
putdocx table table4(.,.), font("Times New Roman", 9)

// Export Table 4
putdocx save "$tables_path/Table4.docx", replace
